<template>
  <div>
    <el-card>
      <!-- sku表格展示区域 -->
      <el-table border style="margin: 10px 0" :data="skuList">
        <!-- sku序号区域 -->
        <el-table-column
          label="序号"
          type="index"
          align="center"
          width="80px"
        ></el-table-column>
        <!-- sku名称区域 -->
        <el-table-column
          label="名称"
          width="150px"
          prop="skuName"
        ></el-table-column>
        <!-- sku描述区域 -->
        <el-table-column
          label="描述"
          prop="skuDesc"
          min-width="300"
        ></el-table-column>
        <!-- sku图片区域 -->
        <el-table-column label="默认图片" width="150px">
          <template #="{ row }">
            <el-image
              style="width: 100%; height: 100%"
              fit="scale-down"
              :src="row.skuDefaultImg"
            />
          </template>
        </el-table-column>
        <!-- sku重量区域 -->
        <el-table-column
          label="重量(克)"
          prop="weight"
          width="150px"
        ></el-table-column>
        <!-- sku价格区域 -->
        <el-table-column
          label="价格(元)"
          prop="price"
          width="150px"
        ></el-table-column>
        <!-- sku操作区域 -->
        <el-table-column label="操作" width="260px" fixed="right">
          <!-- sku商品操作上架与下架区域 -->
          <template #="{ row }">
            <el-tooltip
              effect="dark"
              :content="row.isSale === 0 ? 'Sku商品上架' : 'Sku商品下架'"
              placement="bottom"
            >
              <el-button
                :type="row.isSale === 0 ? 'success' : 'info'"
                :icon="row.isSale === 0 ? 'Top' : 'Bottom'"
                size="small"
                @click="updateSale(row)"
              />
            </el-tooltip>
            <!-- sku编辑区域 -->
            <el-tooltip effect="dark" content="编辑SKu" placement="bottom">
              <el-button
                type="primary"
                icon="Edit"
                size="small"
                @click="updateSku"
              />
            </el-tooltip>
            <!-- sku查看区域 -->
            <el-tooltip effect="dark" content="Sku商品详情" placement="bottom">
              <el-button
                type="info"
                icon="InfoFilled"
                size="small"
                @click="findSku(row)"
              />
            </el-tooltip>
            <!-- sku删除气泡框区域 -->
            <el-popconfirm
              :title="`确定要删除 ${row.skuName} 吗?`"
              icon="DeleteFilled"
              icon-color="#f56c6c"
              @confirm="deleteSku(row)"
            >
              <template #reference>
                <el-tooltip
                  effect="dark"
                  content="删除Sku商品"
                  placement="bottom"
                >
                  <el-button type="danger" size="small" icon="Delete" />
                </el-tooltip>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        v-model:current-page="pageNo"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 25, 50]"
        :background="true"
        layout="prev, pager, next, jumper, ->, sizes, total"
        :total="total"
        @size-change="changeSize"
        @current-change="changePageNo"
      />
    </el-card>
    <!-- sku商品详情页 -->
    <el-drawer v-model="drawer" direction="rtl" size="35%">
      <template #header>
        <h4>查看商品详情</h4>
      </template>
      <template #default>
        <div v-loading="loading">
          <!-- sku商品详情名称区域 -->
          <el-row :gutter="5" class="mb-15">
            <el-col :span="6">名称</el-col>
            <el-col :span="18" style="line-height: 24px; font-size: 14px">
              {{ skuInfo.skuName }}
            </el-col>
          </el-row>
          <!-- sku商品详情描述区域 -->
          <el-row :gutter="5" class="mb-15">
            <el-col :span="6">描述</el-col>
            <el-col :span="18" style="line-height: 24px; font-size: 14px">
              {{ skuInfo.skuDesc }}
            </el-col>
          </el-row>
          <!-- sku商品详情价格区域 -->
          <el-row :gutter="5" class="mb-15">
            <el-col :span="6">价格</el-col>
            <el-col
              :span="18"
              style="color: #ff5000; line-height: 24px; font-size: 14px"
            >
              {{ skuInfo.price }}
            </el-col>
          </el-row>
          <!-- sku商品详情平台属性区域 -->
          <el-row :gutter="5" class="mb-15">
            <el-col :span="6">平台属性</el-col>
            <el-col :span="18">
              <el-tag
                style="margin: 3px"
                v-for="item in skuInfo.skuAttrValueList"
                :key="item.id"
              >
                {{ item.valueName }}
              </el-tag>
            </el-col>
          </el-row>
          <!-- sku商品详情销售属性区域 -->
          <el-row :gutter="5" class="mb-15">
            <el-col :span="6">销售属性</el-col>
            <el-col :span="18">
              <el-tag
                style="margin: 3px"
                type="success"
                v-for="item in skuInfo.skuSaleAttrValueList"
                :key="item.id"
              >
                {{ item.saleAttrValueName }}
              </el-tag>
            </el-col>
          </el-row>
          <!-- sku商品详情图片区域 -->
          <el-row :gutter="5">
            <el-col :span="6">商品图片</el-col>
            <el-col :span="18">
              <el-carousel trigger="click" height="200px" :interval="2000">
                <el-carousel type="card" height="200px">
                  <el-carousel-item
                    v-for="item in skuInfo.skuImageList"
                    :key="item.id"
                  >
                    <el-image :src="item.imgUrl" fit="fill" />
                  </el-carousel-item>
                </el-carousel>
              </el-carousel>
            </el-col>
          </el-row>
        </div>
      </template>
      <template #footer></template>
    </el-drawer>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
// 引入element-plus信息提示
import { ElMessage } from 'element-plus'
// 引入sku请求
import {
  reqSku,
  reqOnSku,
  reqCancelSKu,
  reqSkuInfo,
  reqDelete,
} from '@/api/product/sku/sku'
// 引入sku数据类型
import type {
  SkuResponseData,
  SkuData,
  skuInfoData,
} from '@/api/product/sku/type'
// 分页器起始页数
let pageNo = ref<number>(1)
// 分页器展示的数据量
let pageSize = ref<number>(5)
// 分页器总页数
let total = ref<number>(0)
// sku商品数据
let skuList = ref<SkuData[]>([])
//抽屉控制 默认隐藏
let drawer = ref<boolean>(false)
let loading = ref<boolean>(false)
// 获取sku商品详情数据数组
let skuInfo = ref<SkuData>({})
// 组件挂载完毕发请求
onMounted(() => {
  reqSkuList()
})

// 获取sku商品数据
const reqSkuList = async (page: number = 1) => {
  pageNo.value = page
  const result: SkuResponseData = await reqSku(pageNo.value, pageSize.value)
  if (result.code === 200) {
    // 获取sku商品
    skuList.value = result.data.records
    // 获取sku商品总页数
    total.value = result.data.total
    ElMessage({
      type: 'success',
      message: 'Sku商品数据加载成功',
    })
  } else {
    ElMessage({
      type: 'error',
      message: 'Sku商品数据加载失败',
    })
  }
}

// 下拉框改变每页显示条数
const changeSize = () => {
  reqSkuList()
}
// 当前页数发生改变的时候
const changePageNo = (page: number) => {
  reqSkuList(page)
}

// 商品上下架操作
const updateSale = async (row: SkuData) => {
  // isSale === 0 未上架; isSale === 1 已上架
  if (row.isSale === 0) {
    const result: any = await reqOnSku(row.id as number)
    if (result.code == 200) {
      ElMessage({
        type: 'success',
        message: 'Sku商品上架成功',
      })
      // 上架成功后再次发起请求
      reqSkuList(pageNo.value)
    }
  } else {
    const result: any = await reqCancelSKu(row.id as number)
    if (result.code == 200) {
      ElMessage({
        type: 'success',
        message: 'Sku商品下架成功',
      })
      // 上架成功后再次发起请求
      reqSkuList(pageNo.value)
    }
  }
}

// 更新sku操作
const updateSku = () => {
  ElMessage.warning('别催，功能还在加班开发')
}

// 查看sku商品详情
const findSku = async (row: SkuData) => {
  loading.value = true
  drawer.value = true
  const result: skuInfoData = await reqSkuInfo(row.id as number)
  if (result.code == 200) {
    // 获取sku商品详情数据
    skuInfo.value = result.data
    loading.value = false
    // 查看商品详情失败的提示
    ElMessage({
      type: 'success',
      message: '查看商品详情成功',
    })
  } else {
    // 查看商品详情失败的提示
    ElMessage({
      type: 'error',
      message: '查看商品详情失败',
    })
  }
}

// 删除sku商品
const deleteSku = async (row: SkuData) => {
  const result: any = await reqDelete(row.id)
  if (result.code == 200) {
    // 再次获取最新的sku商品数据
    reqSkuList(skuList.value.length > 1 ? pageNo.value : pageNo.value - 1)
    // 删除成功的提示
    ElMessage({
      type: 'success',
      message: '删除Sku商品成功',
    })
  } else {
    // 删除失败的提示
    ElMessage({
      type: 'error',
      message: '删除Sku商品失败',
    })
  }
}
</script>

<style lang="scss" scoped>
.mb-15 {
  margin-bottom: 15px;
}
.el-carousel__item h3 {
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}
</style>
